﻿/* 
 * ServersControl.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file acts as a controller to Servers tab.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security;
using System.Diagnostics;

partial class ServersControl : UserControl
{
    // Apply the changes
    private void applyButton_Click(object sender, EventArgs e)
    {

    }

    // Cancel the operations
    private void cancelButton_Click(object sender, EventArgs e)
    {
        Process[] p = Process.GetProcessesByName("mmc");
        p[0].Kill();
    }

    // Show servers
    private void show_Click(object sender, EventArgs e)
    {
        datagrid.Rows.Clear();
        fileServers = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Server Preferences\\File", true);
        volumeServers = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Server Preferences\\VLDB", true);
        if (fileServers != null)
        {
            fileServer = fileServers.GetValueNames();
            fileCount = fileServers.ValueCount;
        }

        if (volumeServers != null)
        {
            volumeServer = volumeServers.GetValueNames();
            volumeCount = volumeServers.ValueCount;
        }
        if (fileCheckBox.Checked)
            for (int i = 0; i < fileCount; i++)
                datagrid.Rows.Add(fileServer.GetValue(i).ToString(), "File Server", fileServers.GetValue(fileServer.GetValue(i).ToString()));
        if (volumeCheckBox.Checked)
            for (int i = 0; i < volumeCount; i++)
                datagrid.Rows.Add(volumeServer.GetValue(i).ToString(), "Volume Server", volumeServers.GetValue(volumeServer.GetValue(i).ToString()));
    }

    // Add/Update a Server
    private void buttonAddUpdate_Click(object sender, EventArgs e)
    {

        if (serverTextBox.Text.Equals(""))
        {
            String error = "Server should be a non-empty string";
            MessageBox.Show(error);
            return;
        }
        if (!checkint(rankTextBox.Text))
        {
            String error = "Rank should be an Integer";
            MessageBox.Show(error);
            return;
        }

        if (fileRadioButton.Checked)
        {
            fileServers.SetValue(serverTextBox.Text, rankTextBox.Text, RegistryValueKind.DWord);
        }
        else if (volumeRadioButton.Checked)
        {
            volumeServers.SetValue(serverTextBox.Text, rankTextBox.Text, RegistryValueKind.DWord);
        }
        fileServers = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Server Preferences\\File", true);
        volumeServers = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Server Preferences\\VLDB", true);

        if (fileServers != null)
        {
            fileServer = fileServers.GetValueNames();
            fileCount = fileServers.ValueCount;
        }

        if (volumeServers != null)
        {
            volumeServer = volumeServers.GetValueNames();
            volumeCount = volumeServers.ValueCount;
        }

        datagrid.Rows.Clear();
        if (fileCheckBox.Checked)
            for (int i = 0; i < fileCount; i++)
                datagrid.Rows.Add(fileServer.GetValue(i).ToString(), "File Server", fileServers.GetValue(fileServer.GetValue(i).ToString()));
        if (volumeCheckBox.Checked)
            for (int i = 0; i < volumeCount; i++)
                datagrid.Rows.Add(volumeServer.GetValue(i).ToString(), "Volume Server", volumeServers.GetValue(volumeServer.GetValue(i).ToString()));
    
    }

    // Remove a server
    private void buttonRemove_Click(object sender, EventArgs e)
    {

        if (serverTextBox.Text.Equals(""))
        {
            String error = "Server Name should be a nonempty string";
            MessageBox.Show(error);
            return;
        }
        if (!checkint(rankTextBox.Text))
        {
            String error = "Rank Name should be an Integer";
            MessageBox.Show(error);
            return;
        }
        
        fileServers = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Server Preferences\\File", true);
        volumeServers = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Server Preferences\\VLDB", true);

        if (fileServers != null)
        {
            fileServer = fileServers.GetValueNames();
            fileCount = fileServers.ValueCount;
        }

        if (volumeServers != null)
        {
            volumeServer = volumeServers.GetValueNames();
            volumeCount = volumeServers.ValueCount;
        }

        int index = -1, i = 0;
        if (fileRadioButton.Checked)
        {
            for (i = 0; i < fileCount; i++) if (serverTextBox.Text.Equals(fileServer.GetValue(i).ToString())) index = i;

            if ((index != -1) && (rankTextBox.Text.Equals(fileServers.GetValue(serverTextBox.Text).ToString())))
            {
                fileServers.DeleteValue(serverTextBox.Text, false);
            }
        }

        else if (volumeRadioButton.Checked)
        {
            for (i = 0; i < volumeCount; i++) if (serverTextBox.Text.Equals(volumeServer.GetValue(i).ToString())) index = i;

            if ((index != -1) && (rankTextBox.Text.Equals(volumeServers.GetValue(serverTextBox.Text).ToString())))
            {
                volumeServers.DeleteValue(serverTextBox.Text, false);
            }
        }
        fileServers = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Server Preferences\\File", true);
        volumeServers = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OpenAFS\\Client\\Server Preferences\\VLDB", true);

        if (fileServers != null)
        {
            fileServer = fileServers.GetValueNames();
            fileCount = fileServers.ValueCount;
        }

        if (volumeServers != null)
        {
            volumeServer = volumeServers.GetValueNames();
            volumeCount = volumeServers.ValueCount;
        }
        datagrid.Rows.Clear();
        if (fileCheckBox.Checked)
            for (i = 0; i < fileCount; i++)
                datagrid.Rows.Add(fileServer.GetValue(i).ToString(), "File Server", fileServers.GetValue(fileServer.GetValue(i).ToString()));
        if (volumeCheckBox.Checked)
            for (i = 0; i < volumeCount; i++)
                datagrid.Rows.Add(volumeServer.GetValue(i).ToString(), "Volume Server", volumeServers.GetValue(volumeServer.GetValue(i).ToString()));
    }

    private bool checkint(string a)
    {
        bool flag = true;

        if (a.Equals("")) flag = false;

        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] < 48 || a[i] > 57) flag = false;
        }
        return flag;
    }
}